/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.StudentDTO;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author 11520_000
 */
public class StudentDAL {

    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private StudentDTO m_StudentObj;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String m_sqlCmd;

    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constuctor">
    public StudentDAL() {
        this.m_StudentObj = new StudentDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();
        this.m_sqlCmd = "";
    }
   //</editor-fold>  

    //<editor-fold defaultstate="collapsed" desc="Method">
    /**
     * Ham lay thong cua tat ca cac Sinh Vien
     *
     * @return boolean
     */
    public ResultSet getAllInfoStudent() {
        try {
            m_sqlCmd = "{Call GetAllInfoStudentFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * Ham get danh sach tat ca cac sinh trong bang SINHVIEN
     *
     * @return
     */
    public ResultSet getAllStudent() {
        try {
            m_sqlCmd = "{Call GetAllStudentFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getRegistrationCoursesInforOfStudent(Object[] _value){
        try
        {
            m_sqlCmd = "{Call GetRegistrationCoursesInforOfStudent(?,?,?,?,?,?,?)}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,_value);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
        

    public ResultSet getAllPriorityObjectOfStudentByIdFromDatabase(String _id) {
        try {
            m_sqlCmd = "{Call GetInfoPriorityObjectByIdStudentFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public ResultSet getStudentById(String _id) {
        try {
            m_sqlCmd = "{Call GetStudentByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public int getStudentIdMaxFromDatabase(String _input) {
        int maxID = 0;

        try {
            m_sqlCmd = "{Call GetMaxIdStudentFromDatabase(?)}";
            String[] arrId = {_input};

            ResultSet rs = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);

            while (rs.next()) {
                maxID = Integer.parseInt(rs.getString(1));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return maxID;
    }
    
      public ResultSet getAllAdmissionYearFromDatabase(){
        
        try {
            m_sqlCmd = "{Call GetAllAdmissionYearFromDatabase}";
            String[] arrId = {};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * Ham insert mot sinh vien xuong database
     *
     * @param _studentObj
     * @return
     */
    public boolean insertStudentToDatabase(StudentDTO _studentObj) {
        try {
            m_sqlCmd = "{Call InsertStudentToDatabase(?,?,?,?,?,?,?,?,?)}";
            Object[] arrStr = {_studentObj.getStudentID(), _studentObj.getFullName(), _studentObj.getGender(),
                _studentObj.getDayOfBirth(), _studentObj.getDistrict(), _studentObj.getProvince(),
                _studentObj.getMajorID(), _studentObj.getAdmissionYear(), _studentObj.getStateID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean updateStudentToDatabase(StudentDTO _studentObj) {
        try {
            m_sqlCmd = "{Call UpdateStudentToDatabase(?,?,?,?,?,?,?,?,?)}";
            Object[] arrStr = {_studentObj.getStudentID(), _studentObj.getFullName(), _studentObj.getGender(),
                _studentObj.getDayOfBirth(), _studentObj.getDistrict(), _studentObj.getProvince(),
                _studentObj.getMajorID(), _studentObj.getAdmissionYear(), _studentObj.getStateID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean deleteStudentFromDatabase(StudentDTO _studentObj) {
        try {
            m_sqlCmd = "{Call DeleteStudentFromDatabase(?)}";
            String[] arrStr = {_studentObj.getStudentID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //Lay tat ca thong tin cua sinh vien theo dieu kien cho truoc
       public ResultSet getAllStudentByConditionFromDatabase(Object[] _value){
        try
        {
            m_sqlCmd = "{Call [GetAllStudentByConditionFromDatabase](?,?,?,?,?)}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,_value);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    //Phương thức này nằm trong chi tiết học phần
     public ResultSet GetAllSubjectByRegistrationCoursesID(Object[] _value){
        try
        {
            m_sqlCmd = "{Call GetAllSubjectByRegistrationCoursesID(?,?,?)}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,_value);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
     
     public ResultSet getAllListScoreStudentBySubjectSemesterYear(String _idSubject, String _idYear, String _idSemester) {
        try {
            m_sqlCmd = "{Call GetAllListScoreStudentBySubjectSemesterYearFromDatabase(?,?,?)}";
            String[] arrStr = {_idSubject, _idYear, _idSemester};
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrStr);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getInfoStudentById(String _id) {
        try {
            m_sqlCmd = "{Call GetInfoStudentByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
   //</editor-fold>
